package com.haiyi.software.app;

import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.haiyi.software.app.basic.FragmentBasicInformation;
import com.haiyi.software.entity.EntityMsg;
import com.haiyi.software.entity.UserFriend;
import com.haiyi.software.tools.L;
import com.haiyi.software.tools.MyTools;
import com.haiyi.software.xmpp.event.XMPPConnectionState;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListDelegate;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshListView.PullToRefreshListAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class FragmentInformation extends FragmentBasicInformation implements
		OnItemClickListener, PullToRefreshListDelegate {

	private ImageLoader imageLoader;

	private DisplayImageOptions options;

	private View viewFragmentInformation;

	private PullToRefreshListView mPullRefreshListView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		super.onCreateView(inflater, container, savedInstanceState);

		this.viewFragmentInformation = inflater.inflate(
				R.layout.fragment_information, null);

		// 初始化
		initComp();
		//

		handler.sendEmptyMessageDelayed(1, 800);

		return viewFragmentInformation;
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			mPullRefreshListView.setRefreshing();
		};
	};

	@Override
	public void onResume() {
		super.onResume();
		requestNetworkData();
	};

	private void initComp() {

		// TODO 自动生成的方法存根

		// listview
		mPullRefreshListView = (PullToRefreshListView) viewFragmentInformation
				.findViewById(R.id.list_informations);

		mPullRefreshListView.setScrollingWhileRefreshingEnabled(false);

		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {

					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						// TODO 自动生成的方法存根
						requestNetworkData();
					}
				});

		mPullRefreshListView.setOnItemClickListener(this);
		// 图片控件
		this.imageLoader = ImageLoader.getInstance();
		this.options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(20)).build();

	}

	private static class ViewHolder {

		ImageView imageViewPhoto;

		TextView textViewTitle;

		TextView textViewContent;

		TextView textViewInfoCount;

		TextView textViewTime;

		Button btnPhoto;
	}

	@Override
	public synchronized void requestFinish() {

		mPullRefreshListView.setPullToRefreshListDelegate(this);
		mPullRefreshListView.setDataSource(data);
		mPullRefreshListView.setAdapter(null);
		mPullRefreshListView.getAdapterEx().notifyDataSetChanged();
		mPullRefreshListView.onRefreshComplete();

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO 自动生成的方法存根
		Map<String, Object> mapInfo = (Map<String, Object>) parent
				.getItemAtPosition(position);

		// TODO 自动生成的方法存根
		Intent chatIntent = new Intent(getActivity(), HiChat.class);

		UserFriend friend = new UserFriend();

		// 用户名
		friend.setUfName(mapInfo.get("name").toString());

		String photoString = "";

		if (mapInfo.get("uhead") != null) {

			photoString = mapInfo.get("uhead").toString();
		}

		// 头像
		friend.setUfPhoto(photoString);

		// UID
		friend.setUfID(mapInfo.get("hi_id").toString());

		Bundle bundleUF = new Bundle();

		bundleUF.putSerializable("com.hichat.UserFriend", friend);

		chatIntent.putExtras(bundleUF);

		startActivityForResult(chatIntent, 10018);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO 自动生成的方法存根
		switch (requestCode) {
		case 10018:
			requestNetworkData();
			break;

		}
	}

	@Override
	public View bindView(PullToRefreshListAdapter adapter, final int position,
			View convertView, ViewGroup parent) {

		L.i("data=" + data.toString());

		ViewHolder viewHolder = null;

		if (convertView == null) {

			convertView = adapter.getLayoutInflater().inflate(
					R.layout.item_infomation, null);

			viewHolder = new ViewHolder();

			viewHolder.imageViewPhoto = (ImageView) convertView
					.findViewById(R.id.item_i_photo);
			viewHolder.textViewContent = (TextView) convertView
					.findViewById(R.id.item_i_content);
			viewHolder.textViewInfoCount = (TextView) convertView
					.findViewById(R.id.item_i_info_count);
			viewHolder.textViewTime = (TextView) convertView
					.findViewById(R.id.item_i_time);
			viewHolder.textViewTitle = (TextView) convertView
					.findViewById(R.id.item_i_title);
			viewHolder.btnPhoto = (Button) convertView
					.findViewById(R.id.item_n_btn);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		String str_xmlBody = data.get(position).get("body").toString();

		EntityMsg msg = MyTools.getInstance().getWhichEntityMsgByXMLBody(
				str_xmlBody);

		msg.setAllMessage(str_xmlBody);

		viewHolder.textViewContent.setText(msg.getBody());

		if (data.get(position).get("cnt").toString().equals("0")) {

			viewHolder.textViewInfoCount.setText("");
			viewHolder.textViewInfoCount.setVisibility(View.GONE);

		} else {
			viewHolder.textViewInfoCount.setText(data.get(position).get("cnt")
					.toString()
					+ "条未读消息");
			viewHolder.textViewInfoCount.setVisibility(View.VISIBLE);
		}

		viewHolder.textViewTime.setText(MyTools.getInstance()
				.beautifulDateTime(data.get(position).get("times").toString()));

		viewHolder.textViewTitle.setText(data.get(position).get("name")
				.toString());

		viewHolder.btnPhoto.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				Log.i("btnCLICK", data.get(position).get("hi_id").toString());

				Intent intent = new Intent();
				intent.setClass(getActivity(), ActivityPersonal.class);
				Bundle theUID = new Bundle();
				theUID.putString("uid", data.get(position).get("hi_id")
						.toString());
				intent.putExtras(theUID);
				startActivity(intent);
			}
		});

		L.i("img=" + str_server + data.get(position).get("uhead").toString());

		imageLoader.displayImage(str_server
				+ data.get(position).get("uhead").toString(),
				viewHolder.imageViewPhoto, options);
		// TODO 自动生成的方法存根
		return convertView;
	}

	@Override
	protected void requestError() {
		// TODO 自动生成的方法存根

	}

	@Override
	public void XMPPConnectionStateListener(XMPPConnectionState args) {
		// TODO 自动生成的方法存根

	}
}
